<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class IntegralRule extends Model
{
	
    use SoftDeletes;

    protected $table = 'integral_rule';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];


    /**
     * 增加积分统一处理方法（签到除外）
     * @param int $rule_id
     * @param int $user_id
     * @param int $obj_id
     * @param int $types
     * @return bool|string[]
     */
    public static function rule($rule_id = 0, $user_id = 0, $obj_id = 0, $types = 1)
    {
        /* 1获得规则 */
        $integral_rule = self::where('status', 1)->find($rule_id);
        if (!$integral_rule || $integral_rule->integral == 0) {
            return false;
        }

        /* 2验证能不能加分 */
        $check_where = [
            'user_id'      => $user_id,
            'obj_source'   => $rule_id,
        ];
        $check_day_where = date('Y-m-d'); // 当天

        $save_state = false; // 是否可执行
        if ($integral_rule->day_time_type == 1) {
            // 每X天X次
            $finish_count = IntegralRecord::where($check_where)->whereDate('created_at', $check_day_where)->count();
            if($finish_count < $integral_rule -> every_day_num){
                $save_state = true;
            }
        } else {
            // 只给一次
            $finish_count = IntegralRecord::where($check_where)->first();
            if (!$finish_count) {
                $save_state = true;
            }
        }

        /* 3给用户加分、完善加分记录 */
        DB::beginTransaction();
        $save_res = false;
        if ($save_state) {
            $record = [
                'integral_num' => $integral_rule->integral,
                'types' => $types,
                'obj_id' => $obj_id,
                'explain' => $integral_rule->intro,
            ];
            $record = array_merge($record, $check_where);
            $save_res = IntegralRecord::store($record);
        }

        if ($save_res) {
            DB::commit();
            return ['msg' => ($types ==1 ? '积分奖励 +' : '积分扣除 -').$integral_rule->integral];
        } else {
            DB::rollBack();
            return false;
        }

    }

    /**
     * 签到奖励处理
     * @param int $rule_id 11每日签到；12累计签到奖励
     * @param int $clock_in_day_rule_id 签到规则ID
     * @param int $user_id
     * @param string $month_time 签到时间
     * @param int $integral 奖励分数
     */
    public static function clockIn($rule_id = 0, $clock_in_day_rule_id = 0, $user_id = 0, $month_time = '', $integral = 0)
    {
        $integral_rule = self::where('status', 1)->find($rule_id);
        if (!$integral_rule) {
            return false;
        }

        DB::beginTransaction();
        /* 完善签到记录 */
        $clock_record = [
            'user_id' => $user_id,
            'clock_in_day_rule_id' => $clock_in_day_rule_id,
            'integral' => $integral,
            'intro' => $integral_rule->intro,
            'type' => $rule_id == 12 ? 1 : 2, // 1每日规则
            'day_time' => $month_time
        ];
        $clock_record = ClockInDayRecord::create($clock_record);
        if (!$clock_record) {
            return false;
        }

        /* 给用户加分、完善加分记录 */
        $record = [
            'integral_num' => $integral,
            'types' => 1, // 收入
            'obj_id' => $clock_record->id,
            'explain' => $integral_rule->intro,
            'user_id' => $user_id,
            'obj_source' => $rule_id
        ];
        $save_res = IntegralRecord::store($record);

        if ($save_res) {
            DB::commit();
            return true;
        } else {
            DB::rollBack();
            return false;
        }

    }
}
